package com.qys.livesMall.app.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.app.user.mapper.AppUserFriendMapper;
import com.qys.livesMall.app.user.mapper.H2AppUserMapper;
import com.qys.livesMall.app.user.model.dto.AppBaseDTO;
import com.qys.livesMall.app.user.model.dto.sys.UserFriendEventDTO;
import com.qys.livesMall.app.user.model.entity.AppUserConcern;
import com.qys.livesMall.app.user.model.entity.AppUserFriend;
import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.app.user.model.vo.app.AppUserConcernVO;
import com.qys.livesMall.app.user.service.AppUserConcernService;
import com.qys.livesMall.app.user.service.AppUserFriendService;
import com.qys.livesMall.framework.view.BaseQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户好友 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-08
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppUserFriendServiceImpl extends ServiceImpl<AppUserFriendMapper, AppUserFriend> implements AppUserFriendService {

    private final AppUserConcernService userConcernService;
    private final H2AppUserMapper appUserMapper;


    /**
     * 用户好友关系新增/修改
     *
     * @param dto {@link UserFriendEventDTO}
     */
    @Override
    public void saveUserFriend(UserFriendEventDTO dto) {
        //判断用户是否互相关注
//        boolean concern = checkConcern(dto.getUserId(), dto.getFriendId());
//        if (!concern) {
//            log.error("当前用户没有互相关注:{}", dto);
//            return;
//        }

        QueryWrapper<AppUserFriend> wrapper = Wrappers.<AppUserFriend>query()
                .eq("user_id", dto.getUserId())
                .eq("friend_id", dto.getFriendId());
        AppUserFriend userFriend = baseMapper.selectOne(wrapper);

        //关注
        if (dto.isConcern()) {
            if (null == userFriend) {
                userFriend = new AppUserFriend();
                userFriend.setFriendId(dto.getFriendId());
                userFriend.setUserId(dto.getUserId());

                H2AppUser h2AppUser = appUserMapper.selectById(dto.getFriendId());
                if (null != h2AppUser) {
                    userFriend.setFriendAvatar(h2AppUser.getAvatar());
                    userFriend.setUserName(h2AppUser.getNickName());
                }
                userFriend.setStatus((byte) 1);
                userFriend.setConcernTime(LocalDateTime.now());
                baseMapper.insert(userFriend);
            } else {
                userFriend.setStatus((byte) 1);
                userFriend.setConcernTime(LocalDateTime.now());
                baseMapper.update(userFriend, Wrappers.<AppUserFriend>lambdaUpdate().eq(AppUserFriend::getUserId, dto.getUserId()).eq(AppUserFriend::getFriendId, dto.getFriendId()));
            }
        }
        //取消关注
        else {
            if (null == userFriend) {
                log.error("未获取到用户好友相关信息:{}", dto);
                return;
            }
            Wrapper<AppUserFriend> updateWrapper = Wrappers.<AppUserFriend>update()
                    .eq("user_id", dto.getUserId())
                    .eq("friend_id", dto.getFriendId());
            AppUserFriend appUserFriend = new AppUserFriend();
            appUserFriend.setStatus((byte) 0);
            baseMapper.update(appUserFriend, updateWrapper);
        }
    }

    /**
     * 判断用户是否互相关注
     *
     * @param userId
     * @param friendId
     * @return
     */
    private boolean checkConcern(Long userId, Long friendId) {
        AppUserConcern concern = userConcernService.getOne(Wrappers.<AppUserConcern>query().eq("user_Id", userId)
                .eq("concern_id", friendId).eq("status", 1));

        AppUserConcern concern1 = userConcernService.getOne(Wrappers.<AppUserConcern>query().eq("user_Id", friendId)
                .eq("concern_id", userId).eq("status", 1));

        return null != concern1 && null != concern;
    }

    /**
     * 查询用户好友列表
     *
     * @param baseQuery
     */
    @Override
    public Page<AppUserConcernVO> pageUserFriend(BaseQuery baseQuery) {
        //查询好友信息
        Page<AppUserConcernVO> page = new Page<>(baseQuery.getPageNum(), baseQuery.getPageSize());
        page.setRecords(baseMapper.listFriend(page, baseQuery));
        return page;
    }
}
